<template>
  <app-card>
    <template #header>
      <el-input
          v-model="listParams.name"
          placeholder="请输入昵称"
          clearable
          @clear="loadList"
          style="width: 300px"
      >
        <template #append>
          <el-button @click="loadList">
            <el-icon>
              <search/>
            </el-icon>
          </el-button>
        </template>
      </el-input>
      <el-button type="primary" @click="formVisible = true">
        添加用户
      </el-button>
    </template>
    <el-table
        :data="list"
        v-loading="listLoading"
        @sort-change="handleSortChange"
        stripe
        style="width: 100%"
    >
      <el-table-column fixed="left" type="selection" width="55"/>
      <el-table-column align="center" width="110px" prop="avatar" label="头像">
        <template #default="{row}">
          <app-avatar :size="40" :src="row.avatarUrl"></app-avatar>
        </template>
      </el-table-column>
      <el-table-column align="center" min-width="150px" prop="openId" label="用户编号"/>
      <el-table-column align="center" min-width="150px" prop="nickName" label="昵称"/>
      <el-table-column align="center" width="90px" prop="sex" label="性别"/>
      <el-table-column align="center" width="130px" prop="loginCount" label="连接" />
      <el-table-column align="center" width="130px" prop="status" label="状态">
        <template #default="{row}">
          <el-tag v-if="row.status==='1'" type="success">启用</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" width="170px" prop="createdAt" sortable="custom" label="创建日期"/>
      <el-table-column fixed="right" align="center" width="160px" label="编辑">
        <template #default="{row}">
          <el-button type="text" @click="handleUpdate(row.id)">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <app-pagination
        v-model:page="listParams.pageNum"
        v-model:limit="listParams.pageSize"
        :list-count="listCount"
        :load-list="loadList"
        :disabled="listLoading"
    />
    <user-edit v-model="formVisible"
               v-model:id="userId"
               @success="handleFormSuccess"
    ></user-edit>
  </app-card>
</template>

<script lang="ts" setup>
import {onMounted, reactive, ref} from 'vue';
import UserEdit from './edit.vue'
import {IListParams} from "@/api/types/common";
import {ICustomerInfo} from "@/api/types/customer";
import {queryCustomerAccount} from "@/api/customer";

const formVisible = ref(false);
const userId = ref<number | null>(null)
const list = ref<ICustomerInfo[]>([]);
const listCount = ref(0);
const listLoading = ref(false);
const listParams = reactive<IListParams>({
  pageNum: 1,
  pageSize: 10,
  username:''
});

onMounted(() => {
  loadList();
});

const loadList = async () => {
  listLoading.value = true;
  const data = await queryCustomerAccount(listParams).finally(() => {
    listLoading.value = false;
  });
  list.value = data.rows;
  listCount.value = data.total;
};

// 排序
const handleSortChange = ({prop, order}: {
  prop: string; order: string;
}) => {
  listParams.sortField = prop;
  listParams.sortOrder = order
  loadList();
};

const handleUpdate = (id: number) => {
  userId.value = id;
  formVisible.value = true;
};

const handleFormSuccess = () => {
  formVisible.value = false;
  loadList();
};



</script>

<style lang="scss" scoped>
</style>
